﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		HomeController.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Text;
using System.Web.Mvc;
using Dexter.Logger.Contracts;
using Dexter.Search;
using Dexter.Services;
using Dexter.Web.Mvc.Filters;
using Dexter.Web.Mvc.Results;
using Dexter.Web.Mvc.ViewModel.FrontEnd.HomeController;
using Dexter.Web.Utils;

namespace Dexter.Web.Mvc.Controllers.FrontEnd {
	[DexterCallFilter ( true )]
	public class HomeController : PublicControllerBase {
		public HomeController ( ISearchService searchService , ICommentService commentService , IReferrerService referrerService , IConfigurationService configurationService , IWebUtils webUtils , ILogger logger , IUrlBuilder urlBuilder , IBlogRollService blogRollService , ICategoryService categoryService , IPostService postService , IThemeManager themeManager , ITagService tagService , IPageService pageService ) : base ( searchService , commentService , referrerService , configurationService , webUtils , logger , urlBuilder , blogRollService , categoryService , postService , themeManager , tagService , pageService ) {}

		[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
		public ActionResult Index ( int? id , int? page ) {
			int pageIndex = page ?? 1;

			pageIndex--;

			var data = base.GetViewData ( new HomeIndexViewData ( base.WebUtils , base.Configuration , base.UrlBuilder ) );
			data.Posts = PostService.GetPublishedPostDto ( pageIndex , base.Configuration.ElementInListsConfiguration.NumberOfElementsInList );

			data.SetMetaKeywords ( base.GetKeywords ( data.Posts.Result ) );

			return View ( data );
		}

		[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
		[OutputCache ( VaryByParam = "id" , Duration = 600 )]
		public ActionResult GetMediaResource ( string id ) {
			try {
				return new ThemeFileResult ( id , base.CurrentTheme );
			}
			catch ( Exception e ) {
				Logger.Error ( e.Message , e );
				return new HttpNotFoundResult ( );
			}
		}

		[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
		public ActionResult OpenSearch() {
			var sb = new StringBuilder ( "<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\"> " );
			sb.AppendFormat ( "<ShortName>{0}</ShortName>", Configuration.BlogName );
			sb.AppendFormat ( "<Description>{0}</Description>", Configuration.SeoConfiguration.DefaultDescription );
			sb.AppendFormat ( "<Image height=\"16\" width=\"16\" type=\"image/vnd.microsoft.icon\">{0}</Image>", UrlBuilder.Favicon );
			sb.AppendFormat ( "<Url type=\"text/html\" template=\"{0}blog/search?q={1}\" /> ", UrlBuilder.HomePage, "{searchTerms}" );
			sb.AppendFormat ( "<Url type=\"application/rss+xml\" template=\"{0}blog/search?q={1}\" /> ", UrlBuilder.HomePage, "{searchTerms}" );
			sb.Append ( "</OpenSearchDescription>" );

			HttpContext.Response.ContentEncoding = Encoding.UTF8;
			AddHeaders ( "text/xml", 30, sb.GetHashCode ( ) );

			return Content ( sb.ToString ( ) , "text/xml" );
		}
	}
}
